package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"io"
	"os"
	"path/filepath"
	"strings"
	"system/cache"
	"system/models"
	"time"
)

type BookController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Data interface{} `json:"date"`
	Msg  string      `json:"msg"`
}

func (c *BookController) Add() {
	Prodects := c.GetString("prodects")
	types := c.GetString("types")
	chapter := c.GetString("chapter")
	author := c.GetString("author")
	file, m, _ := c.GetFile("img")
	if m.Size >= 1024*1024*5 {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "图片大小不能超过5mb",
		}
		c.ServeJSON()
		return
	}
	ext := filepath.Ext(m.Filename)
	suffix := ".jpg.png"
	if !strings.Contains(suffix, ext) {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "图片格式只能为jpg和png格式",
		}
		c.ServeJSON()
		return
	}
	openFile, _ := os.OpenFile("static/img/"+m.Filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	io.Copy(openFile, file)
	if Prodects == "" || types == "" || author == "" {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "参数不能为空",
		}
		c.ServeJSON()
		return
	}
	book := models.Book{
		Prodects: Prodects,
		Types:    types,
		Chapter:  chapter,
		Author:   author,
		Img:      "http://127.0.0.1:8080/static/img/" + m.Filename,
	}
	err := models.DB.Debug().Create(&book).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "上传失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Data: nil,
			Msg:  "上传成功",
		}
		c.ServeJSON()
		return
	}
}
func (c *BookController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	i := (page - 1) * size
	var book []models.Book
	var count int
	models.DB.Model(&models.Book{}).Pluck("count(*)", &count)
	get, _ := cache.Rdb.Get(cache.Ctx, "book_list").Result()
	if get != "" {
		json.Unmarshal([]byte(get), &book)
	} else {
		models.DB.Debug().Offset(i).Limit(size).Find(&book)
		marshal, _ := json.Marshal(&book)
		cache.Rdb.Set(cache.Ctx, "book_list", marshal, time.Minute*2)
	}
	c.Data["json"] = Res{
		Code: 200,
		Data: struct {
			T interface{}
			C int
		}{
			book, count,
		},
		Msg: "展示成功",
	}
	c.ServeJSON()
	return
}
func (c *BookController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	var book models.Book
	err := models.DB.Debug().Where("id=?", id).Delete(&book).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "删除失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Data: nil,
			Msg:  "删除成功",
		}
		c.ServeJSON()
		return
	}
}
func (c *BookController) Show() {
	id, _ := c.GetInt("id")
	var book []models.Book
	err := models.DB.Debug().Where("id=?", id).First(&book).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 500,
			Data: nil,
			Msg:  "查找失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Data: book,
			Msg:  "查找成功",
		}
		c.ServeJSON()
		return
	}
}
